<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
            
    function getPromise(url) {
        return new Promise(function (resolve,reject) {
                //1 创建对象
                var ajax = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject();
                //2 建立连接
                //get post 
                ajax.open('get',url,true);//false:同步  true 异步
                //3 发送请求 去
                ajax.send();
                //4接收数据  只要服务器和客户端发送数据交互，一定会触发这个onreadystatechange事件
                ajax.onreadystatechange = function () {
                    
                    if( ajax.readyState == 4 ){
                        if( ajax.status == 200 ){
                            resolve( ajax.responseText )
                        }else{
                            reject(ajax.status);
                        };
                    };
                };

        });
    }
    getPromise('https://www.baidu.com').then(function (data) {
        console.log(data);
    })
    </script>
</body>
</html>

